﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpecFlowTfsLinker.Entities;

namespace SpecFlowTfsLinker.Printer
{
    [Serializable]
    public class FeaturePrinter : ISpecFlowPrinter
    {
        private readonly UserStory _us;

        public FeaturePrinter(UserStory userStory)
        {
            _us = userStory;
        }

        private static string Display(TestCase testCase)
        {
            var builder = new StringBuilder();
            builder.AppendLine("Scenario: " + testCase.Name);
            builder.Append(DisplayStatementList(testCase.Given, StatementType.Given));
            builder.Append(DisplayStatementList(testCase.When, StatementType.When));
            builder.Append(DisplayStatementList(testCase.Then, StatementType.Then));
            return builder.ToString();
        }

        private static string DisplayStatementList(List<Statement> statement, StatementType type)
        {
            var builder = new StringBuilder();
            statement.ForEach(currentStatement =>
                                       {
                                           var statementPrinter = new StatementPrinter(currentStatement);
                                           if (currentStatement == statement.First())
                                               builder.AppendLine(type + " " + statementPrinter.DisplayStatement);
                                           else builder.AppendLine(StatementType.And + " " + statementPrinter.DisplayStatement);
                                       });
            return builder.ToString();
        }

        public string Print()
        {
            var builder = new StringBuilder();
            builder.AppendLine("Feature: " + _us.Title);
            builder.AppendLine(_us.Description);
            builder.AppendLine();
            _us.TestCases.ForEach(testCase => builder.AppendLine(Display(testCase)));
            return builder.ToString().Trim();
        }
    }


}